<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="申请人(主创)学号" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="申请人(主创)学号"></el-input>
    </el-form-item>
    <el-form-item label="申请内容" prop="txt">
      <el-input v-model="dataForm.txt" placeholder="申请内容"></el-input>
    </el-form-item>
    <el-form-item label="申请材料地址" prop="ossUrl">
      <el-input v-model="dataForm.ossUrl" placeholder="申请材料地址"></el-input>
    </el-form-item>
    <el-form-item label="提交时间" prop="tijiaoTime">
      <el-input v-model="dataForm.tijiaoTime" placeholder="提交时间"></el-input>
    </el-form-item>
    <el-form-item label="审批时间" prop="shenpiTime">
      <el-input v-model="dataForm.shenpiTime" placeholder="审批时间"></el-input>
    </el-form-item>
    <el-form-item label="审批人" prop="shenpiUser">
      <el-input v-model="dataForm.shenpiUser" placeholder="审批人"></el-input>
    </el-form-item>
    <el-form-item label="是否通过1:通过 0:待通过 -1:未通过" prop="pass">
      <el-input v-model="dataForm.pass" placeholder="是否通过1:通过 0:待通过 -1:未通过"></el-input>
    </el-form-item>
    <el-form-item label="项目类型id" prop="xiangmuId">
      <el-input v-model="dataForm.xiangmuId" placeholder="项目类型id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          txt: '',
          ossUrl: '',
          tijiaoTime: '',
          shenpiTime: '',
          shenpiUser: '',
          pass: '',
          xiangmuId: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '申请人(主创)学号不能为空', trigger: 'blur' }
          ],
          txt: [
            { required: true, message: '申请内容不能为空', trigger: 'blur' }
          ],
          ossUrl: [
            { required: true, message: '申请材料地址不能为空', trigger: 'blur' }
          ],
          tijiaoTime: [
            { required: true, message: '提交时间不能为空', trigger: 'blur' }
          ],
          shenpiTime: [
            { required: true, message: '审批时间不能为空', trigger: 'blur' }
          ],
          shenpiUser: [
            { required: true, message: '审批人不能为空', trigger: 'blur' }
          ],
          pass: [
            { required: true, message: '是否通过1:通过 0:待通过 -1:未通过不能为空', trigger: 'blur' }
          ],
          xiangmuId: [
            { required: true, message: '项目类型id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/stu/shenqingroup/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.shenqinGroup.userId
                this.dataForm.txt = data.shenqinGroup.txt
                this.dataForm.ossUrl = data.shenqinGroup.ossUrl
                this.dataForm.tijiaoTime = data.shenqinGroup.tijiaoTime
                this.dataForm.shenpiTime = data.shenqinGroup.shenpiTime
                this.dataForm.shenpiUser = data.shenqinGroup.shenpiUser
                this.dataForm.pass = data.shenqinGroup.pass
                this.dataForm.xiangmuId = data.shenqinGroup.xiangmuId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/stu/shenqingroup/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'txt': this.dataForm.txt,
                'ossUrl': this.dataForm.ossUrl,
                'tijiaoTime': this.dataForm.tijiaoTime,
                'shenpiTime': this.dataForm.shenpiTime,
                'shenpiUser': this.dataForm.shenpiUser,
                'pass': this.dataForm.pass,
                'xiangmuId': this.dataForm.xiangmuId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
